package com.scf.guaranty.controller;


import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.github.pagehelper.PageInfo;
import com.scf.guaranty.entity.Guaranty;
import com.scf.guaranty.entity.UploadResult;
import com.scf.guaranty.service.GuarantyService;
import com.scf.uitl.ResponseResult;
import com.scf.uitl.ResultStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 担保物管理表(Guaranty)表控制层
 *
 * @author makejava
 * @since 2021-11-12 17:12:50
 */
@RestController
@CrossOrigin
@RequestMapping("/guaranty")
public class GuarantyController {
    /**
     * 服务对象
     */
    @Resource
    private GuarantyService guarantyService;

    @Resource
    private OSSClient ossClient;

    private static final String BUCKETNAME = "java81";
    private String baseUrl = "https://java81.oss-cn-beijing.aliyuncs.com/";

    @ResponseBody
    @GetMapping("/getAll/{pageNum}/{pageSize}")
    public ResponseResult<PageInfo<Guaranty>> getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        ResponseResult<PageInfo<Guaranty>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setData(guarantyService.getAll(pageNum, pageSize));
        return responseResult;
    }

    @PostMapping("/upload")
    public ResponseResult upload(@RequestParam("file") MultipartFile[] files, @RequestParam("guaranty") String guaranty) {
        System.out.println(files.length);
        JSONObject jsonObject = JSONObject.parseObject(guaranty);
        Guaranty g = JSONObject.toJavaObject(jsonObject, Guaranty.class);
        System.out.println(g);
        ResponseResult responseResult = new ResponseResult();
        //1.获得文件名字
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            //得到后置名
            String suffix = fileName.substring(fileName.indexOf("."));
            //新名字
            fileName = UUID.randomUUID().toString() + suffix;
            //2.获取文件流
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                //
                ossClient.putObject(BUCKETNAME, fileName, inputStream);
                //设置文件有效期  60天
                Date date = new Date(new Date().getTime() + 1000 * 3600 * 24 * 60);
                //获取url
                URL url = ossClient.generatePresignedUrl(BUCKETNAME, fileName, date);
                //返回数据
                UploadResult fileResult = new UploadResult(fileName, baseUrl + fileName);
                g.setAccessory(baseUrl + fileName);
                //
            } catch (IOException e) {
                responseResult.setMessage("上传失败");
                responseResult.setCode(500);
                e.printStackTrace();
                return responseResult;
            }
        }
        try {
            guarantyService.add(g);
            responseResult.setCode(200);
            responseResult.setMessage("上传成功");
        } catch (Exception e) {
            responseResult.setCode(500);
            responseResult.setMessage("上传失败");
            e.printStackTrace();
            return responseResult;
        }
        return responseResult;
    }
    @PostMapping("/update")
    public ResponseResult update(@RequestParam("file") MultipartFile[] files, @RequestParam("guaranty") String guaranty) {
        System.out.println(files.length);
        JSONObject jsonObject = JSONObject.parseObject(guaranty);
        Guaranty g = JSONObject.toJavaObject(jsonObject, Guaranty.class);
        System.out.println(g);
        ResponseResult responseResult = new ResponseResult();
        //1.获得文件名字
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            //得到后置名
            String suffix = fileName.substring(fileName.indexOf("."));
            //新名字
            fileName = UUID.randomUUID().toString() + suffix;
            //2.获取文件流
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                //
                ossClient.putObject(BUCKETNAME, fileName, inputStream);
                //设置文件有效期  60天
                Date date = new Date(new Date().getTime() + 1000 * 3600 * 24 * 60);
                //获取url
                URL url = ossClient.generatePresignedUrl(BUCKETNAME, fileName, date);
                //返回数据
                UploadResult fileResult = new UploadResult(fileName, baseUrl + fileName);
                g.setAccessory(baseUrl + fileName);
                //
            } catch (IOException e) {
                responseResult.setMessage("上传失败");
                responseResult.setCode(500);
                e.printStackTrace();
                return responseResult;
            }
        }
        try {
            guarantyService.update(g);
            responseResult.setCode(200);
            responseResult.setMessage("上传成功");
        } catch (Exception e) {
            responseResult.setCode(500);
            responseResult.setMessage("上传失败");
            e.printStackTrace();
            return responseResult;
        }
        return responseResult;
    }

    @ResponseBody
    @GetMapping("/getByCondition")
    public ResponseResult<PageInfo<Guaranty>> getByCondition(String name, String type, Integer pageNum, Integer pageSize) {
        System.out.println(name + type + pageNum + pageSize);
        ResponseResult<PageInfo<Guaranty>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setData(guarantyService.getByCondition("".equals(name) ? null : name, "0".equals(type) ? null : type, pageNum, pageSize));
        return responseResult;
    }


    @ResponseBody
    @GetMapping("/getById/{id}")
    public ResponseResult<List<Guaranty>> getById(@PathVariable int id) {
        ResponseResult<List<Guaranty>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setData(guarantyService.getById(id));
        return responseResult;
    }

    @GetMapping("/getByApproval")
    public ResponseResult<PageInfo<Guaranty>> getByApproval(String status, String name, String type, Integer pageNum, Integer pageSize) {
        System.out.println(status + name + type + pageNum + pageSize);
        ResponseResult<PageInfo<Guaranty>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setStatus(ResultStatus.SUCCESS);
        responseResult.setData(guarantyService.getByApproval("0".equals(status) ? null : status, "".equals(name) ? null : name, "0".equals(type) ? null : type, pageNum, pageSize));
        return responseResult;
    }

    @GetMapping("/del/{id}")
    public ResponseResult del(@PathVariable Integer id) {
        ResponseResult responseResult = new ResponseResult<>();
        try {
            guarantyService.deleteById(id);
            responseResult.setCode(200);
            responseResult.setStatus(ResultStatus.SUCCESS);
            responseResult.setMessage("删除成功");
        } catch (Exception e) {
            responseResult.setCode(500);
            responseResult.setMessage("删除失败");
            e.printStackTrace();
            return responseResult;
        }
        return responseResult;
    }
    @GetMapping("/pass/{id}")
    public ResponseResult pass(@PathVariable Integer id){
        ResponseResult responseResult = new ResponseResult<>();
        try {
            guarantyService.pass(id);
            responseResult.setCode(200);
            responseResult.setStatus(ResultStatus.SUCCESS);
        }catch (Exception e){
            responseResult.setCode(500);
            responseResult.setMessage("失败");
            e.printStackTrace();
            return responseResult;
        }
        return responseResult;
    }
    @GetMapping("/reject/{id}")
    public ResponseResult  reject(@PathVariable Integer id){
        ResponseResult responseResult = new ResponseResult<>();
        try {
            guarantyService.reject(id);
            responseResult.setCode(200);
            responseResult.setStatus(ResultStatus.SUCCESS);
        }catch (Exception e){
            responseResult.setCode(500);
            responseResult.setMessage("失败");
            e.printStackTrace();
            return responseResult;
        }
        return responseResult;
    }
}

